const express = require('express');
const router = express.Router();

// 实体类
const Response = require('../entity/http/Response');
const ObjId = require('../entity/common/ObjId');
const UserInfo = require('../entity/user/UserInfo');
const Img64 = require('../entity/file/Img64');

// 业务层
const userService = require('../service/userService');

/**
 * @接口名称 获取用户头像接口
 * @用途 根据小程序用户的头像id来获取头像预签名URL
 * @有效时长 10小时
 */
router.get('/avater', async function(req, res, next) {
    console.log(`[log] [req-id=${req.reqId}] 进入了 /avater 接口`);
    // 读取请求参数
    let obj = new ObjId(req.query, 'id');
    // 判断参数是否合法
    let flag = obj.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    console.log(`[log] [req-id=${req.reqId}] 参数校验成功`);
    // 调用业务层
    userService.getAvater(obj.id).then((result) => {
        console.log(`[log] [req-id=${req.reqId}] 响应 result`, result);
        res.status(result.code).json(result);
    }).catch((err) => {
        console.log(`[log] [req-id=${req.reqId}] 服务器错误`, err);
        res.status(500).json((new Response()).fail(500, "资源不可访问"));
    });
});

/**
 * @接口名称 获取用户信息接口
 * @用途 根据 小程序用户id 来获取用户信息
 */
router.get('/userInfo', async function(req, res, next) {
    console.log(`[log] [req-id=${req.reqId}] 进入了 /userInfo 接口`);
    // 读取请求参数
    let obj = new ObjId(req.query, 'id');
    // 判断参数是否合法
    let flag = obj.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    console.log(`[log] [req-id=${req.reqId}] 参数校验成功`);
    // 调用业务层
    userService.getUserInfo(obj.id).then((result) => {
        console.log(`[log] [req-id=${req.reqId}] 响应 result`, result);
        res.status(result.code).json(result);
    }).catch((err) => {
        console.log(`[log] [req-id=${req.reqId}] 服务器错误`, err);
        res.status(500).json((new Response()).fail(500, "资源不可访问"));
    });
});

/**
 * @接口名称 修改用户信息接口
 * @参数 { name: string, gender: string, phone: string, email: string, bio: string, school: string, major: string, grade: string }
 */
router.put('/userInfo', async function(req, res, next) {
    console.log(`[log] [req-id=${req.reqId}] 进入了 /userInfo 接口`);
    let uid = req.jwtResult.data.uid;
    // 读取请求参数
    let userInfo = new UserInfo(req.body);
    // 判断参数是否合法
    let flag = userInfo.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    console.log(`[log] [req-id=${req.reqId}] 参数校验成功`);
    // 调用业务层
    userService.setUserInfo(uid, userInfo).then((result) => {
        console.log(`[log] [req-id=${req.reqId}] 响应 result`, result);
        res.status(result.code).json(result);
    }).catch((err) => {
        console.log(`[log] [req-id=${req.reqId}] 服务器错误`, err);
        res.status(500).json((new Response()).fail(500, "资源不可访问"));
    });
});

/**
 * @接口名称 修改用户头像接口
 * @参数 { avatar: string }
 */
router.put('/avatar', async function(req, res, next) {
    console.log(`[log] [req-id=${req.reqId}] 进入了 /avater 接口`);
    let uid = req.jwtResult.data.uid;
    // 读取请求参数
    let avatar = new Img64({ img: req.body.avatar });
    // 判断参数是否合法
    let flag = avatar.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    console.log(`[log] [req-id=${req.reqId}] 参数校验成功`);
    // 调用业务层
    userService.setAvatar(uid, avatar).then((result) => {
        console.log(`[log] [req-id=${req.reqId}] 响应 result`, result);
        res.status(result.code).json(result);
    }).catch((err) => {
        console.log(`[log] [req-id=${req.reqId}] 服务器错误`, err);
        res.status(500).json((new Response()).fail(500, "资源不可访问"));
    });
});

module.exports = router;
